#!/bin/bash

echo "=== Cursor MCP 自动配置脚本 ==="

# 检测操作系统
if [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS
    CURSOR_CONFIG_DIR="$HOME/Library/Application Support/Cursor/User"
    CURSOR_CONFIG_FILE="$CURSOR_CONFIG_DIR/settings.json"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
    # Linux
    CURSOR_CONFIG_DIR="$HOME/.config/Cursor/User"
    CURSOR_CONFIG_FILE="$CURSOR_CONFIG_DIR/settings.json"
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
    # Windows
    CURSOR_CONFIG_DIR="$APPDATA/Cursor/User"
    CURSOR_CONFIG_FILE="$CURSOR_CONFIG_DIR/settings.json"
else
    echo "❌ 不支持的操作系统: $OSTYPE"
    exit 1
fi

echo "📁 Cursor配置目录: $CURSOR_CONFIG_DIR"
echo "📄 配置文件: $CURSOR_CONFIG_FILE"

# 检查配置文件是否存在
if [ ! -f "$CURSOR_CONFIG_FILE" ]; then
    echo "📝 创建新的配置文件..."
    mkdir -p "$CURSOR_CONFIG_DIR"
    echo '{}' > "$CURSOR_CONFIG_FILE"
fi

# 备份原配置文件
BACKUP_FILE="$CURSOR_CONFIG_FILE.backup.$(date +%Y%m%d_%H%M%S)"
cp "$CURSOR_CONFIG_FILE" "$BACKUP_FILE"
echo "💾 已备份原配置文件到: $BACKUP_FILE"

# MCP配置
MCP_CONFIG='{
  "mcp.servers": {
    "mysql": {
      "type": "http",
      "url": "http://localhost:8099/mcp/call",
      "headers": {
        "Content-Type": "application/json"
      }
    }
  }
}'

# 检查是否已经配置了MCP
if grep -q "mcp.servers" "$CURSOR_CONFIG_FILE"; then
    echo "⚠️  检测到已存在MCP配置，将更新MySQL配置..."
    
    # 使用Python更新配置
    python3 -c "
import json
import sys

try:
    with open('$CURSOR_CONFIG_FILE', 'r', encoding='utf-8') as f:
        config = json.load(f)
    
    if 'mcp.servers' not in config:
        config['mcp.servers'] = {}
    
    config['mcp.servers']['mysql'] = {
        'type': 'http',
        'url': 'http://localhost:8099/mcp/call',
        'headers': {'Content-Type': 'application/json'}
    }
    
    with open('$CURSOR_CONFIG_FILE', 'w', encoding='utf-8') as f:
        json.dump(config, f, indent=2, ensure_ascii=False)
    
    print('✅ MCP配置已更新')
    
except Exception as e:
    print(f'❌ 更新配置失败: {e}')
    sys.exit(1)
"
else
    echo "📝 添加MCP配置..."
    
    # 使用Python添加配置
    python3 -c "
import json
import sys

try:
    with open('$CURSOR_CONFIG_FILE', 'r', encoding='utf-8') as f:
        config = json.load(f)
    
    config['mcp.servers'] = {
        'mysql': {
            'type': 'http',
            'url': 'http://localhost:8099/mcp/call',
            'headers': {'Content-Type': 'application/json'}
        }
    }
    
    with open('$CURSOR_CONFIG_FILE', 'w', encoding='utf-8') as f:
        json.dump(config, f, indent=2, ensure_ascii=False)
    
    print('✅ MCP配置已添加')
    
except Exception as e:
    print(f'❌ 添加配置失败: {e}')
    sys.exit(1)
"
fi

echo ""
echo "🎉 配置完成！"
echo ""
echo "📋 下一步操作："
echo "1. 重启Cursor"
echo "2. 打开命令面板 (Cmd/Ctrl + Shift + P)"
echo "3. 搜索 'MCP' 查看可用工具"
echo ""
echo "🧪 测试方法："
echo "在Cursor中直接输入："
echo "  '请帮我测试数据库连接'"
echo "  '请列出数据库中的所有表'"
echo "  '请查询sys_user表的前5条数据'"
echo ""
echo "📁 客户端文件："
echo "- mysql-mcp-client.js (Node.js客户端)"
echo "- simple_mysql_client.py (Python客户端)"
echo ""
echo "🔗 服务地址: http://localhost:8099"
echo "📊 健康检查: http://localhost:8099/health" 